package cq.zhcqk.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.dou361.dialogui.DialogUIUtils;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cq.zhcqk.R;
import cq.zhcqk.adapter.DyxxAdapter;
import cq.zhcqk.adapter.DzbJiaGouAdapter;
import cq.zhcqk.base.BaseActivity;
import cq.zhcqk.util.Common;
import cq.zhcqk.util.GsonUtil;
import cq.zhcqk.util.StringUtil;

/**
 * Created by zhangle on 2017/12/6.
 */
public class DY_DyxxActivity extends BaseActivity implements View.OnClickListener{

    private List<Map<String,Object>> list;
    private DyxxAdapter dyxxAdapter;
    private ListView listView;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_dy_dyxx);
        initBack();
        initListView();
    }

    @Override
    protected void initData() {
        getDetail();
    }

    private void initBack(){
        ImageView back = (ImageView) findViewById(R.id.header_back);
        TextView title = (TextView) findViewById(R.id.header_text);
        title.setText("党员学习情况");
        back.setOnClickListener(this);
    }

    private void initListView(){
        listView = (ListView) findViewById(R.id.dyxx_list);
        list = new ArrayList<>();
        dyxxAdapter = new DyxxAdapter(context,list);
        listView.setAdapter(dyxxAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context,DY_Dyxx_DetailActivity.class);
                intent.putExtra("title",list.get(position).get("title")+"");
                intent.putExtra("cid", StringUtil.stringToInt(list.get(position).get("cid")+""));
                startActivity(intent);
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.header_back:
                finish();
                break;
        }
    }

    private void getDetail(){
        buildBean.show();
        RequestParams params = new RequestParams();
        httpClient.post(Common.URL+Common.STUDYLIST,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                System.out.println("onSuccess："+response);
                Map<String, Object> map = GsonUtil.GsonToMaps(response+"");
                List<Map<String,Object>> listNew = (List<Map<String, Object>>) map.get("data");
                list.addAll(listNew);
                dyxxAdapter = new DyxxAdapter(context,list);
                listView.setAdapter(dyxxAdapter);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable,
                        errorResponse);
                System.out.println("errorResponse："+errorResponse);
                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogUIUtils.dismiss(buildBean);
            }
        });
    }
}
